//
//  EmptyTipsView.m
//  Pods
//
//  Created by molon on 15/9/9.
//
//

#import "EmptyTipsView.h"
#import "MLKit.h"

@interface EmptyTipsView()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *label;

@end

@implementation EmptyTipsView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageViewSideLength = 50.0f;
        [self addSubview:self.imageView];
        [self addSubview:self.label];
    }
    return self;
}

#pragma mark - getter
- (UIImageView *)imageView
{
    if (!_imageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        _imageView = imageView;
    }
    return _imageView;
}

- (UILabel *)label
{
    if (!_label) {
        UILabel *label = [UILabel new];
        label.text = @"空空如也～";
        label.font = BOLDFONT(14.0f);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor darkGrayColor];
        
        _label = label;
    }
    return _label;
}

#pragma mark - setter
- (void)setImageViewSideLength:(CGFloat)imageViewSideLength
{
    _imageViewSideLength = imageViewSideLength;
    
    [self setNeedsLayout];
}

- (void)setYOffset:(CGFloat)yOffset
{
    _yOffset = yOffset;
    
    [self setNeedsLayout];
}

#pragma mark - layout
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat imageViewSide = self.imageViewSideLength;
    if (self.imageView.hidden||!self.imageView.image) {
        imageViewSide = 0.0f;
    }
    
#define kPadding 10.0f
    CGFloat labelHeight = [self.label.text heightForWidth:self.frameWidth-kPadding*2 withFont:self.label.font];
    
    self.imageView.frame = CGRectMake((self.frameWidth-imageViewSide)/2, (self.frameHeight-labelHeight-imageViewSide-kPadding)/2+self.yOffset, imageViewSide, imageViewSide);
    
    self.label.frame = CGRectMake(kPadding, self.imageView.frameBottom+kPadding, self.frameWidth-kPadding*2,fmin(labelHeight, self.frameHeight-self.imageView.frameBottom-kPadding));
}


@end
